import yaml

yaml.warnings({'YAMLLoadWarning': False})
from util.user_log import log

# from ruamel import yaml

fp = ''
fp_ele = ''

class OperationYaml(object):
    def __init__(self, path=None):
        pass

    # 读取文件
    def read_data(self, path=fp):
        with open(path, encoding="utf-8") as file_01:
            result = yaml.load(file_01)
        return result

    # 写json,str格式
    def write_data(self, data, path=fp):
        log.info('准备写入更新后数据')
        with open(path, 'w', encoding='utf-8') as fi:
            yaml.dump(data, fi, allow_unicode=True)
            log.info('写入成功')

    def get_data(self, key, path=fp):
        data = self.read_data(path)[key]
        ids_list = []
        for list_01 in data:
            s1 = ' | '
            ids = '\n' + s1.join(map(str, list_01))
            ids_list.append(ids)
        return data, ids_list

    def element(self, data, key01, key02):
        value = data[key01][key02]
        value = value.split('--')
        value = map(lambda x: x.strip(), value)
        return tuple(value)

    def page_ele(self, key):
        data = self.read_data(fp_ele)[key]
        l01 = []
        for value in data.values():
            value = value.split('--')
            value = tuple(map(lambda x: x.strip(), value))
            if len(value) not in [1, 2, 3]:
                raise Exception('元素错误：{}；'
                                '\n必须为1至3位，这里是{}位'.format(value, len(value)))
            l01.append(value)
        return l01


op_yaml = OperationYaml()
if __name__ == '__main__':
    data = op_yaml.page_ele('order_form')
    for i in data:
        print(i)
    pass
    # a02 = op_yaml.element(data, 'order_form', 'addressDel')
    # print(a02)
    # print(type(a02))
